<?php
/**
 * @package Curly
 * @version 0.9
 * @link http://curly.codeplex.com/
 * @license http://curly.codeplex.com/license The MIT License
 * @author Dawid Zawada
 */

namespace Curly
{
    /**
     * Class representing a set of proxy options
     * 
     * @package Curly
     * @license http://curly.codeplex.com/license The MIT License
     */
    class Proxy
    {
        /**
         * Proxy server type
         * 
         * @var Curly\ProxyType
         */
        protected $type;
        
        /**
         * Proxy server host
         * 
         * @var string
         */
        protected $host;
        
        /**
         * Proxy server port
         * 
         * @var int
         */
        protected $port;
        
        /**
         * Proxy server login
         * 
         * @var string
         */
        protected $login;
        
        /**
         * Proxy server password
         * 
         * @var string
         */
        protected $password;
        
        /**
         * Creates a new Proxy object
         * 
         * @internal
         */
        public function __construct()
        {
            $this->setType( ProxyType::None() );
        }
        
        /**
         * Returns the proxy server type
         *
         * @return Curly\ProxyType
         */
        public function getType()
        {
            return $this->type;
        }
        
        /**
         * Sets the proxy server type
         * 
         * @param Curly\ProxyType $type
         */
        public function setType( ProxyType $type )
        {
            $this->type = $type;
        }
        
        /**
         * Returns the proxy server host
         *
         * @return string
         */
        public function getHost()
        {
            return $this->host;
        }
        
        /**
         * Returns the proxy server port
         *
         * @return int
         */
        public function getPort()
        {
            return $this->port;
        }
        
        /**
         * Returns the proxy server host and port 
         * with a colon between them
         * 
         * If the port is not set, the method will return the host only
         * It the host is not set too, the method will return an empty string
         *
         * @return string
         */
        public function getHostPort()
        {
            if( $this->getHost() != "" )
            {
                $fullAddress = $this->getHost();
                if( $this->getPort() != 0 )
                {
                    $fullAddress .= ":" . $this->getPort();
                }
                
                return $fullAddress;
            }
            else
            {
                return "";
            }
        }
        
        /**
         * Sets the proxy server host and port
         * 
         * Port can also be included in the $host param (e.g. 'server.com:1234'),
         * but it will be ignored if the $port param is set.
         * 
         * @param string $host
         * @param int $port
         */
        public function setHostPort( $host, $port = null )
        {
            // extract port number if the $host param contains one
            // and the $port param is not set
            if( ( $pos = strpos( $host, ":" ) ) !== false )
            {
                if( $port === null )
                {
                    list( $host, $port ) = explode( ":", $host, 2 );
                }
                else
                {
                    // the $port param is set and has higher priority, 
                    // we remove the port part from the $host param 
                    $host = substr( $host, 0, $pos );
                }
            }
            
            $this->host = (string)$host;
            $this->port = (int)$port;
            
            // Set default proxy server type if needed
            if( $this->getHost() != "" && $this->getType() == ProxyType::None() )
            {
                $this->setType( ProxyType::HTTP() );
            }
        }
        
        /**
         * Returns the proxy server login
         *
         * @return string
         */
        public function getLogin()
        {
            return $this->login;
        }
        
        /**
         * Returns the proxy server password
         *
         * @return string
         */
        public function getPassword()
        {
            return $this->password;
        }
        
        /**
         * Returns the proxy server login and password
         * with a colon between them
         * 
         * If the login and password are not set, the method
         * will return an empty string
         *
         * @return string
         */
        public function getLoginPassword()
        {
            if( $this->getLogin() != "" || $this->getPassword() != "" )
            {
                return $this->getLogin() . ":" . $this->getPassword();
            }
            else
            {
                return "";
            }
        }
        
        /**
         * Sets the proxy server login and password
         * 
         * @param string $login
         * @param string $password
         */
        public function setLoginPassword( $login, $password )
        {
            $this->login = (string)$login;
            $this->password = (string)$password;
        }
    }
}